package com.ruoyi.service.impl;

import com.ruoyi.domain.CategoryOne;
import com.ruoyi.domain.CategoryThree;
import com.ruoyi.domain.CategoryTwo;
import com.ruoyi.mapper.CategoryOneMapper;
import com.ruoyi.mapper.CategoryThreeMapper;
import com.ruoyi.mapper.CategoryTwoMapper;
import com.ruoyi.service.ICategoryOneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class CategoryOneServiceImpl implements ICategoryOneService {

    @Autowired
    private CategoryOneMapper categoryOneMapper;

    @Autowired
    private CategoryTwoMapper categoryTwoMapper;

    @Autowired
    private CategoryThreeMapper categoryThreeMapper;

    @Override
    public List<CategoryOne> selectAll() {
        return categoryOneMapper.selectList();
    }

    @Override
    public CategoryOne selectById(Long id) {
        return categoryOneMapper.selectByPrimaryKey(id);
    }

    @Override
    public int insertOne(CategoryOne categoryOne) {
        return categoryOneMapper.insertSelective(categoryOne);
    }

    @Override
    public int updateById(CategoryOne categoryOne) {
        return categoryOneMapper.updateByPrimaryKeySelective(categoryOne);
    }

    @Override
    public int deleteByIds(Long[] ids) {
        return categoryOneMapper.deleteByPrimaryKey(ids);
    }

    @Override
    public List<CategoryOne> selectPage(CategoryOne categoryOne) {
        return categoryOneMapper.selectPage(categoryOne);
    }

    @Override
    public List<Map<String, Object>> selectListCascater() {
        List<CategoryOne> categoryOnes = categoryOneMapper.selectList();
        List<CategoryTwo> categoryTwos = categoryTwoMapper.selectList();
        List<CategoryThree> categoryThrees = categoryThreeMapper.selectList();

        List<Map<String, Object>> data = categoryOnes.stream()
                .map(categoryOne -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("value", categoryOne.getConeId());
                    map.put("label", categoryOne.getConeName());
                    List<Map<String, Object>> twodata = categoryTwos.stream()
                            .filter(categoryTwo -> categoryOne.getConeId().equals(categoryTwo.getConeId()) )
                            .map(categoryTwo -> {
                                Map<String, Object> twoMap = new HashMap<>();
                                twoMap.put("value", categoryTwo.getCtwoId());
                                twoMap.put("label", categoryTwo.getCtwoName());
                                List<Map<String, Object>> threedata = categoryThrees.stream()
                                        .filter(categoryThree -> categoryTwo.getCtwoId().equals(categoryThree.getCtwoId()))
                                        .map(categoryThree -> {
                                            Map<String, Object> threeMap = new HashMap<>();
                                            threeMap.put("value", categoryThree.getCthrId());
                                            threeMap.put("label", categoryThree.getCthrName());
                                            return threeMap;
                                        }).collect(Collectors.toList());
                                twoMap.put("children", threedata);
                                return twoMap;
                            }).collect(Collectors.toList());
                    map.put("children", twodata);
                    return map;
                }).collect(Collectors.toList());

        return data;
    }
}
